<?php

/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2017/4/26
 * Time: 10:42
 */

namespace App\Http\Controllers\Order;

use App\Http\Controllers\Controller;

class RequisitionController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('check.login');
    }

    /**
     * 列表页面
     */
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('sale.requisition.list',compact('toolBarButton','menu_id'));
    }

    public function listData()
    {
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 10);
        try{
            $listTree = callApiServer("Order.RequisitionService.getReqList",$params);

            return response()->json($listTree);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function getGoodsList()
    {
        $params['id'] = request('req_id');
        try{
            $listTree = callApiServer("Order.RequisitionService.getReqGoods",$params);
            return response()->json($listTree);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function showAddForm()
    {
//        $menu_id = request('curr_menu_id', 0);
//        if(!$menu_id){
//            return response()->json('非法操作');
//        }
//        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('sale.requisition.form');
    }

    public function showGoodsAdd()
    {
        return view('sale.requisition.goods');
    }


    public function showEditForm()
    {
        $params['req_id'] = request('req_id');
        try{
            $info = callApiServer("Order.RequisitionService.getReqOne", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('sale.requisition.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function audit()
    {
        $params['req_id'] = request('req_id');
        $params['doc_state'] = request('doc_state');

        $rs = callApiServer("Order.RequisitionService.editState", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>'删除失败']);
    }

    //保存认购单信息
    public function saveReq()
    {
        $params['id'] = request('req_id');
        $params['customer_id'] = request('customer_id');
        $params['customer_mobile'] = request('customer_mobile');
        $params['customer_name'] = request('customer_name');
//        $params['description'] = request('description');
        $warehouse_id = request('warehouse_id');
        $params['shipping_methods'] = request('shipping_methods');
        $shipping_methods = request('shipping_methods');
        if ($params['shipping_methods'] == 1) {
            $params['shipping_address'] = '';
            $params['warehouse_id'] = $warehouse_id;
        }else{
            $params['shipping_address'] = $shipping_methods;
            $params['warehouse_id'] = 0;
        }

        if(empty(request()->all())){
            return response()->json(['code'=>400,'data'=>'组织名称不能为空']);
        }

        $rs = callApiServer("Order.RequisitionService.saveReqInfo", $params);
        if($rs){
            return response()->json(['code'=>200,'id'=>$rs]);
        }
        return response()->json(['code'=>400,'data'=>'插入失败']);
    }

    //添加认购单商品
    public function saveGoods()
    {
        $params['req_id'] = request('req_id');
        $params['goods_id'] = request('goods_id');
        $params['goods_price'] = request('goods_price');
        $params['goods_num'] = request('goods_num');

        if(empty($params['req_id'])){
            return response()->json(['code'=>400,'data'=>'添加信息有误，缺少认购单信息']);
        }

        $rs = callApiServer("Order.RequisitionService.saveReqGoodsInfo", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function remove()
    {
        $params['id'] = request('id');
        $rs = callApiServer("System.OrgService.remove", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>'删除失败']);
    }

}